package com.shouhuobao.take.active;

import android.os.Bundle;
import android.os.Message;
import android.text.Html;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;
import com.shouhuobao.take.active.adapter.ActiveRankListAdapter;
import com.shouhuobao.take.model.ActiveBean;
import com.shouhuobao.take.model.ActiveRankListBean;
import com.shouhuobao.take.utils.StringUtils;

import java.util.ArrayList;

/**
 * Created by joker on 2017/9/21.
 * 活动榜单activity
 */
public class ActiveActivity extends BaseActivity2 {

    private ActiveRankListAdapter mAdapter = null;
    //前三名布局
    private TextView mFirstMoneyTV, mSecondMoneyTV, mThirdMoneyTV;
    private TextView mFirstContentTV, mSecondContentTV, mThirdContentTV;
    //榜单日期
    private ListView mRankListView;
    //我的排名
    private TextView mMineRankTV;
    private TextView mMineParcelCount;

    private ActiveBean activeBean = null;
    private int activeType = 0;//0 进行中 1 历史

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        activeBean = (ActiveBean) getIntent().getSerializableExtra("item");
        activeType = getIntent().getIntExtra("activeType", 0);
        super.onCreate(savedInstanceState);
        if (activeBean != null) {
            showLoadingDialog(null);
            AppHttp.getInstance().queryActiveRankData(activeBean.getType(), activeBean.getWeek(), activeType);
        }
    }

    @Override
    protected void findViewById() {
        setContentView(R.layout.activity_active);
        super.findViewById();
        if (activeBean.getType() == 1) {//代收榜单
            getAppTitle().setCommonTitle("代收荣誉榜");
            ((TextView) findViewById(R.id.title_tv)).setText("周代收量前三驿站可获以下奖品：");
            ((ImageView) findViewById(R.id.active_title_iv)).setImageResource(R.drawable.active_collection_bt);
            ((TextView) findViewById(R.id.week_rank_title_tv)).setText("本周代收荣誉榜");
        } else {//寄件榜单
            getAppTitle().setCommonTitle("寄件荣誉榜");
            ((TextView) findViewById(R.id.title_tv)).setText("周寄件量前三驿站可获以下奖品：");
            ((ImageView) findViewById(R.id.active_title_iv)).setImageResource(R.drawable.active_sent_bt);
            ((TextView) findViewById(R.id.week_rank_title_tv)).setText("本周寄件荣誉榜");
        }
        ((TextView) findViewById(R.id.active_week_tv)).setText("第" + activeBean.getWeek() + "期");
        ((TextView) findViewById(R.id.store_name_tv)).setText("我的驿站");
        mFirstMoneyTV = (TextView) findViewById(R.id.first_money_tv);
        mSecondMoneyTV = (TextView) findViewById(R.id.second_money_tv);
        mThirdMoneyTV = (TextView) findViewById(R.id.third_money_tv);
        mFirstContentTV = (TextView) findViewById(R.id.first_content_tv);
        mSecondContentTV = (TextView) findViewById(R.id.second_content_tv);
        mThirdContentTV = (TextView) findViewById(R.id.third_content_tv);
        mRankListView = (ListView) findViewById(R.id.rank_listview);
        mMineRankTV = (TextView) findViewById(R.id.ranking_tv);
        mMineParcelCount = (TextView) findViewById(R.id.parcel_num_tv);
        mAdapter = new ActiveRankListAdapter(null, getContext());
        mRankListView.setAdapter(mAdapter);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case MsgID.query_active_rank_list_data:
                cancelLoadingDialog();
                final ReqResult<ActiveRankListBean> result = JSON.parser(msg.obj, ActiveRankListBean.class);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (checkLoginStatus(result)) {
                            ActiveRankListBean bean = result.getResult();
                            if (bean == null) {
                                return;
                            }
                            ActiveRankListBean.MineRankBean mineRankBean = bean.getMine();
                            if (mineRankBean != null) {
                                mMineRankTV.setText(mineRankBean.getRank());
                                mMineParcelCount.setText(mineRankBean.getParcelCount());
                            } else {
                                mMineRankTV.setText("暂无排名");
                                mMineParcelCount.setText("0个");
                            }
                            //活动说明介绍
                            ((TextView) findViewById(R.id.active_introduce_tv)).setText(Html.fromHtml(bean.getActiveDeclare()));
                            ((TextView) findViewById(R.id.active_date_tv)).setText(bean.getWeek());
                            updateView(bean.getList());
                            mAdapter.setItems(bean.getList(), true);
                            ((ScrollView) findViewById(R.id.scrollview)).smoothScrollTo(0, 0);
                        } else {
                            showToast(result.getMessage());
                        }
                    }
                });
                return true;
            default:
                return super.handleMessage(msg);
        }
    }

    /**
     * 用来更新一等奖，二等奖，三等奖展示
     *
     * @param items
     */
    private void updateView(ArrayList<ActiveRankListBean.RankListBean> items) {
        if (items == null || items.size() == 0) {
            if (activeBean.getType() == 1) {//代收
                mFirstMoneyTV.setText("38元");
                mFirstContentTV.setText("赠送邮票3张");
                mSecondMoneyTV.setText("28元");
                mSecondContentTV.setText("赠送邮票2张");
                mThirdMoneyTV.setText("18元");
                mThirdContentTV.setText("赠送邮票1张");
            } else {
                mFirstMoneyTV.setText("38元");
                mFirstContentTV.setText("赠送邮票3张");
                mSecondMoneyTV.setText("28元");
                mSecondContentTV.setText("赠送邮票2张");
                mThirdMoneyTV.setText("18元");
                mThirdContentTV.setText("赠送邮票1张");
            }
            return;
        }
        for (int i = 0; i < items.size(); i++) {
            if (i == 0) {//一等奖
                if (StringUtils.isEmpty(items.get(i).getReward1())) {
                    if (activeBean.getType() == 1) {//代收
                        mFirstMoneyTV.setText("38元");
                    } else {
                        mFirstMoneyTV.setText("38元");
                    }
                } else {
                    mFirstMoneyTV.setText(items.get(i).getReward1());
                }
                if (StringUtils.isEmpty(items.get(i).getReward2())) {
                    if (activeBean.getType() == 1) {//代收
                        mFirstContentTV.setText("赠送邮票3张");
                    } else {
                        mFirstContentTV.setText("赠送邮票3张");
                    }
                } else {
                    mFirstContentTV.setText(items.get(i).getReward2());
                }
            } else if (i == 1) {//二等奖
                if (StringUtils.isEmpty(items.get(i).getReward1())) {
                    if (activeBean.getType() == 1) {//代收
                        mSecondMoneyTV.setText("28元");
                    } else {
                        mSecondMoneyTV.setText("28元");
                    }
                } else {
                    mSecondMoneyTV.setText(items.get(i).getReward1());
                }
                if (StringUtils.isEmpty(items.get(i).getReward2())) {
                    if (activeBean.getType() == 1) {//代收
                        mSecondContentTV.setText("赠送邮票2张");
                    } else {
                        mSecondContentTV.setText("赠送邮票2张");
                    }
                } else {
                    mSecondContentTV.setText(items.get(i).getReward2());
                }
            } else if (i == 2) {//三等奖
                if (StringUtils.isEmpty(items.get(i).getReward1())) {
                    if (activeBean.getType() == 1) {//代收
                        mThirdMoneyTV.setText("18元");
                    } else {
                        mThirdMoneyTV.setText("18元");
                    }
                } else {
                    mThirdMoneyTV.setText(items.get(i).getReward1());
                }
                if (StringUtils.isEmpty(items.get(i).getReward2())) {
                    if (activeBean.getType() == 1) {//代收
                        mThirdContentTV.setText("赠送邮票1张");
                    } else {
                        mThirdContentTV.setText("赠送邮票1张");
                    }
                } else {
                    mThirdContentTV.setText(items.get(i).getReward2());
                }
            }
        }
    }
}
